package com.wn55.hrms.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Employee;
import com.wn55.hrms.service.EmployeeService;
import com.wn55.hrms.service.vo.*;
import com.wn55.hrms.util.JwtUtil;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("employee")
@Api(value = "此接口用于操作管理员功能")
public class EmployeeController {
    private EmployeeService employeeService;

    @Value("${avatarPath}")
    private String headerImgPath;

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    JwtUtil jwtUtil = JwtUtil.getInstance();

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ResultResponse.class),
            @ApiResponse(code = 404, message = "NOT FOUND")
    })
    public ResultResponse<Employee> login(
            @ApiParam("接收客户端传过来的账户名和密码封装到本对象")
            @RequestBody Employee manager) {
        ResultResponse<Employee> result = null;
        try {
            Employee m = employeeService.login(manager.getEmpName());
            if (m == null) {
                result = new ResultResponse<>(202, "用户不存在");
            } else {
                if (m.getEmpPassword().equals(manager.getEmpPassword())) {
                    result = new ResultResponse<>(200, "登录成功", m);
                    //此时生成一个token 回应道客户端
                    JwtInfo jwtInfo = new JwtInfo();
                    jwtInfo.setUserName(m.getEmpName());
//                    生成token
                    String token = jwtUtil.createToken(jwtInfo);
                    result.setToken(token);
                } else {
                    result = new ResultResponse<>(203, "密码错误");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "登录失败");
        }
        //返回对象
        return result;
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页查询管理员的列表")
    @Cacheable(value = "employeesPageData",keyGenerator = "keyGenerator")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "currentPage", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "每页显示多少条", required = false, defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的用户名", required = false)
    })
    public ResultResponse<PageInfo<Employee>> list(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            String name) {
        ResultResponse<PageInfo<Employee>> result;
        try {
            PageInfo<Employee> pageInfo = employeeService.listByCondition(currentPage, pageSize, name);
            result = new ResultResponse<>(200, "查询员工成功!", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询员工失败!");
        }
        return result;

    }

    @GetMapping("/listManager")
    @ApiOperation(value = "分页查询管理员的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "currentPage", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "每页显示多少条", required = false, defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的用户名", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pId", value = "员工职位id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "empType", value = "管理员的用户名", required = false)
    })
    public ResultResponse<PageInfo<EmployeeVo>> listManager(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "empName")String name,
            @RequestParam(value = "pId")Integer pId,
            @RequestParam(value = "empType")Integer empType) {
        ResultResponse<PageInfo<EmployeeVo>> result;
        try {
            PageInfo<EmployeeVo> pageInfo = employeeService.listManagerByCondition(currentPage, pageSize, name,empType,pId);
            result = new ResultResponse<>(200, "查询管理员成功!", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询管理员失败!");
        }
        return result;

    }
    @GetMapping("/listEmpByCondition")
    @ApiOperation(value = "根据部门职位级联查询员工")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "deptId", value = "部门id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "positionId", value = "职位id", required = true)
    })
    public ResultResponse<List<EmployeeVo>> listEmpByCondition(
            @RequestParam(value = "deptId")Integer deptId,
            @RequestParam(value = "positionId")Integer positionId) {
        ResultResponse<List<EmployeeVo>> result;
        try {
            List<EmployeeVo> list = employeeService.listEmpByCondition(deptId, positionId);
            result = new ResultResponse<>(200, "级联查询员工成功!", list);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "级联查询员工失败!");
        }
        return result;

    }
    @PutMapping("/editManager")
    public ResultResponse<Void> editManager(@RequestBody EmployeeVo employeeVo){
        ResultResponse<Void> result = null;
        try {
            employeeVo.setUpdateTime(new Date());
            employeeService.modifyManager(employeeVo);
            result = new ResultResponse<>(200,"更新管理员成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"更新管理员失败!");
        }
        return result ;
    }

    @PutMapping("/locked/{eId}/{locked}")
    public ResultResponse<Void> lockedManager(@PathVariable("eId") Integer eId,@PathVariable("locked") Integer locked){
        ResultResponse<Void> result = null;
        try {
            employeeService.lockedManager(eId,locked);
            result = new ResultResponse<>(200,"锁定管理员成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"锁定管理员失败!");
        }
        return result ;
    }

    @PostMapping("/uploadImg")
    @ApiOperation(value = "头像的上传")
    public ResultResponse<String>uploadImg(@ApiParam("接收客户端传过来的头像")
                                           MultipartFile myHeaderImg){
        ResultResponse<String> result =null;
        try{
            //1.获取文件上传的名字
            String filename = myHeaderImg.getOriginalFilename();
            //2.修改图片的名字
            filename = "HRMS"+System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
            //3.获取图片上传路径
            File file = new File(headerImgPath+filename);
            //4.头像上传
            myHeaderImg.transferTo(file);
            result = new ResultResponse<>(200,"头像上传成功",filename);

        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(200,"头像上传失败!");
        }


    return result;
    }

    @PutMapping("/updateEmployee")
    @ApiOperation(value = "修改员工")
    public ResultResponse<Void> update(@RequestBody Employee employee){
        ResultResponse<Void> result =null;
        try {
            employee.setUpdateTime(new Date());
            employeeService.updateEmployee(employee);
            result = new ResultResponse<>(200, "更新成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "更新失败!");
        }
             return result;
    }

    @DeleteMapping("/deleteEmployee/{id}")
    @ApiOperation(value = "删除员工")
    public ResultResponse<Void> deleteEmployee(@PathVariable(value = "id") Long id){
        ResultResponse<Void> result =null;
        try {
            employeeService.deleteEmployee(id);
            result = new ResultResponse<>(200, "删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除失败!");
        }
        return result;
    }
    @PostMapping("/addEmployee")
    public ResultResponse<Void> add(@RequestBody Employee employee){
        ResultResponse<Void> result =null;
        try {
            employee.setLocked("0");
            employee.setUpdateTime(new Date());
            employee.setInsertTime(new Date());
            employee.setProbationPeriodBegin(employee.getJoinTime());
            employeeService.addEmployee(employee);
            result = new ResultResponse<>(200, "添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加失败!");
        }
        return result;
    }
    @GetMapping("getEmpById/{id}")
    public ResultResponse<Employee> getEmpById(@PathVariable("id") Long id){
        ResultResponse<Employee> result = null;
        try {
            Employee e = employeeService.getEmpById(id);
            result = new ResultResponse<>(200, "查找成功",e);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查找失败");
        }
        //返回对象
        return result;
    }





    @GetMapping("/listInternEmployee")
    @ApiOperation(value = "分页查询实习生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "currentPage", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "每页显示多少条", required = false, defaultValue = "4"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "员工的姓名", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pId", value = "员工职位id", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "deptId", value = "员工部门id", required = false)
    })
    @Cacheable(value = "employeePageData",keyGenerator = "keyGenerator")
    public ResultResponse<PageInfo<EmployeeVo>> listInternEmployee(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
            @RequestParam(value = "empName")String name,
            @RequestParam(value = "pId")Integer pId,
            @RequestParam(value = "deptId")Integer deptId) {
        ResultResponse<PageInfo<EmployeeVo>> result;
        try {
            PageInfo<EmployeeVo> pageInfo = employeeService.listInternByCondition(currentPage, pageSize, name,deptId,pId);
            result = new ResultResponse<>(200, "查询实习生成功!", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询实习生失败!");
        }
        return result;

    }

    @GetMapping("/initAgeData")
    @ApiOperation(value = "加载年龄段饼状图")
    @Cacheable(value = "ageData",keyGenerator = "keyGenerator")
    public ResultResponse<AgeVo> initAgeData(){
        ResultResponse<AgeVo> result =null;
        try {
            AgeVo ageVo = employeeService.initAgeData();
            result = new ResultResponse<>(200, "加载成功!",ageVo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "加载失败!");
        }
        return result;
    }

    @GetMapping("/initDeptData")
    @ApiOperation(value = "加载部门饼状图")
    @Cacheable(value = "deptData",keyGenerator = "keyGenerator")
    public ResultResponse<DeptNumVo> initDeptData(){
        ResultResponse<DeptNumVo> result =null;
        try {
            DeptNumVo deptNumVo = employeeService.initDeptData();
            result = new ResultResponse<>(200, "加载成功!",deptNumVo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "加载失败!");
        }
        return result;
    }

    @GetMapping("/initJobStatusData")
    @ApiOperation(value = "加载在职状态饼状图")
    @Cacheable(value = "jobStatusData",keyGenerator = "keyGenerator")
    public ResultResponse<JobStatusVo> initJobStatusData(){
        ResultResponse<JobStatusVo> result =null;
        try {
            JobStatusVo jobStatusVo = employeeService.initJobStatusData();
            result = new ResultResponse<>(200, "加载成功!",jobStatusVo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "加载失败!");
        }
        return result;
    }


    @GetMapping("/initImgData")
    @ApiOperation(value = "加载欢迎页走马图")
    @Cacheable(value = "imgData",keyGenerator = "keyGenerator")
    public ResultResponse<List<EmployeeVo>> initImgData(){
        ResultResponse<List<EmployeeVo>> result =null;
        try {
            List<EmployeeVo> employee = employeeService.initImgData();
            result = new ResultResponse<>(200, "加载成功!",employee);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "加载失败!");
        }
        return result;
    }

}
